<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui">

    <ui:composition template="/template/DefaultTemplate.xhtml">
        <ui:define name="content">
            <h:form>
                <p:panel id="refundrequests" header="Refund Requests" >
                    <p:dataTable value="#{refundRequestEntityController.allRefundRequests}" var="item" 
                                 paginator="true" rows="30" rowsPerPageTemplate="30,50,100">
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListRefundRequestEntityTitle_id}"/>
                            </f:facet>
                            <h:outputText value="#{item.id}"/>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListRefundRequestEntityTitle_salesOrder}"/>
                            </f:facet>
                            <h:outputText value="#{item.salesOrder}"/>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListRefundRequestEntityTitle_amount}"/>
                            </f:facet>
                            <h:outputText value="#{item.amount}"/>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListRefundRequestEntityTitle_status}"/>
                            </f:facet>
                            <h:outputText value="#{item.status}"/>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListRefundRequestEntityTitle_createdAt}"/>
                            </f:facet>
                            <h:outputText value="#{item.createdAt}">
                                <f:convertDateTime pattern="MM/dd/yyyy" />
                            </h:outputText>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListRefundRequestEntityTitle_createdBy}"/>
                            </f:facet>
                            <h:outputText value="#{item.createdBy.fullname}"/>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListRefundRequestEntityTitle_refund}"/>
                            </f:facet>

                            <p:commandLink value="#{item.refund.id}" rendered="#{item.hasRefund()}"
                                           oncomplete="refundDialog.show()" 
                                           update="@form:displayRefund" 
                                           process="@form">
                                <f:setPropertyActionListener target="#{refundRequestEntityController.current}"
                                                             value="#{item}" />
                            </p:commandLink>
                            
                        </p:column>
                        
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="&nbsp;"/>
                            </f:facet>
                            <p:menuButton value="Options" >
                                <p:menuitem value="review" 
                                            update="@form:displayRefundNote" oncomplete="refundNoteDialog.show()"
                                            process="@form"
                                            rendered="#{!item.hasRefund()}">
                                    <f:setPropertyActionListener target="#{refundRequestEntityController.current}" value="#{item}" />
                                </p:menuitem>
                                
                                <p:menuitem value="record refund" 
                                           update="@form:editRefund" oncomplete="recordRefundDialog.show()"
                                           process="@form"
                                           rendered="#{item.refundRequestApproved()}">
                                <f:setPropertyActionListener value="#{item}"  
                                                             target="#{refundRequestEntityController.current}" /> 
                                </p:menuitem>
                            </p:menuButton>
                        </p:column>

                    </p:dataTable>
                </p:panel>

                <p:dialog header="Refund" widgetVar="refundDialog" closable="true" width="600">  
                    <h:panelGroup id="displayRefund" rendered="#{refundRequestEntityController.selected.hasRefund()}">
                        <h:panelGrid columns="2" >
                            <h:outputText value="Status: "/>
                            <h:outputText value="#{refundRequestEntityController.selected.status}" />
                            
                            <h:outputText value="Refund Date: "/>
                            <h:outputText value="#{refundRequestEntityController.selected.refund.refundDate}" >
                                <f:convertDateTime pattern="MM/dd/yyyy" />
                            </h:outputText>
                            
                            <h:outputText value="Refunded by: "/>
                            <h:outputText value="#{refundRequestEntityController.selected.refund.createdBy.fullname}" />
                            
                            <h:outputText value="Note: "/>
                            <h:outputText value="#{refundRequestEntityController.selected.note}" />
                        </h:panelGrid>
                    </h:panelGroup>
                </p:dialog>

                <p:dialog header="Record Refund" widgetVar="recordRefundDialog" closable="false" width="800">  
                    <h:panelGroup id="editRefund" rendered="#{refundRequestEntityController.selected.hasRefund()}">
                        <h:panelGrid columns="2" cellpadding="10">
                            
                            <h:outputText value="Refund Date: " />
                            <p:calendar value="#{refundRequestEntityController.selected.refund.refundDate}" 
                                        maxdate="#{salesQuotationEntityController.getToday()}"/>
                        </h:panelGrid>  
                    </h:panelGroup>
                    <br />
                    <p:commandButton ajax="false" value="Save" action="#{refundRequestEntityController.recordRefund}" />
                    
                </p:dialog>
                
                <p:dialog header="Refund Approval" widgetVar="refundNoteDialog" closable="true" width="400">  
                    <h:panelGroup id="displayRefundNote" >
                        <h:panelGrid columns="2" >
                            <h:outputText value="Status: "/>
                            <h:outputText value="#{refundRequestEntityController.selected.status}" />
                            
                            <h:outputText value="Note: "/>
                            <h:inputTextarea value="#{refundRequestEntityController.selected.note}" />
                        </h:panelGrid>
                    </h:panelGroup>
                    <br />
                    <p:commandButton ajax="false" value="Approve" actionListener="#{refundRequestEntityController.prepareRefundApprove}" action="#{refundRequestEntityController.recordRefundDecision}" />
                    <p:commandButton ajax="false" value="Reject" actionListener="#{refundRequestEntityController.prepareRefundReject}" action="#{refundRequestEntityController.recordRefundDecision}" />
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
